﻿using HAF.Core.Helpers;
using HAF.Core.Datas;
using HAF.LogUtility;
using HAF.LogUtility.Assistantor;
using HAF.WebUtility;
using RDS.Core;
using System;
using System.Text;
using System.Threading.Tasks;

namespace RDS.Author
{
    public class Author
    {
        private Author()
        {

        }

        private static Author _author;
        public static Author GetInstance()
        {
            return _author ?? (_author = new Author());
        }
        /// <summary>
        /// 产品注册
        /// </summary>
        public void Regist()
        {
            LogHelper.WriteInfoLog("开始注册新拷贝");
            try
            {
                //string.Format("{0}{1}{2}{3}{4}", RuntimeController.Device.Mac, RuntimeController.Device.OsVer, RuntimeController.Device.CpuId,
                //RuntimeController.Device.DiskId, RuntimeController.Device.OsVer)
                if (RuntimeAdapter.Terminal != null)
                {
                    Terminal device = RuntimeAdapter.Terminal;
                    device.FingerPrint = GZipStr.GZipCompressString(device.FingerPrint);
                    device.TimeStamp = TimeHelper.GetTimeStamp(true);

                    string atomJson = JsonAssistant.Serialize(device);
                    if (!string.IsNullOrEmpty(atomJson))
                    {
                        Task.Factory.StartNew(() => {
                            string shortJson = atomJson.Replace("TerCode", "tc").Replace("UID", "i").Replace("ProductCode", "pc").Replace("ReqIP", "ri").Replace("Token", "tk").
                            Replace("VER", "v").Replace("DEV", "d").Replace("TimeStamp", "t").Replace("FingerPrint", "fp");
                            string wcfUri = RuntimeAdapter.RegistType == "2" ? "http://172.25.8.14:8081/WasEngine/Audit/Reg" : "http://182.138.102.134:28080/WasEngine/Audit/Reg";
                            string rtnStr = HttpHelper.GetResponseWcf(wcfUri, "POST", shortJson, Encoding.UTF8);
                            LogHelper.WriteInfoLog(rtnStr);
                        });

                        //182.138.102.136 8081  telnet 182.138.102.134 28080
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("采集服务：方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
            }

        }

        /// <summary>
        /// 心跳事件
        /// </summary>
        public void HeartBeat()
        {
            LogHelper.WriteInfoLog("当前拷贝发起存活请求");
            try
            {
                Terminal device = RuntimeAdapter.Terminal;
                device.FingerPrint = GZipStr.GZipCompressString(device.FingerPrint);
                device.TimeStamp = TimeHelper.GetTimeStamp(true);

                string atomJson = JsonAssistant.Serialize(device);
                if (!string.IsNullOrEmpty(atomJson))
                {
                    Task.Factory.StartNew(() =>
                    {
                        string shortJson = atomJson.Replace("TerCode", "tc").Replace("UID", "i").Replace("ProductCode", "pc").Replace("ReqIP", "ri").Replace("Token", "tk").
                        Replace("VER", "v").Replace("DEV", "d").Replace("TimeStamp", "t").Replace("FingerPrint", "fp");
                        string wcfUri = RuntimeAdapter.RegistType == "2" ? "http://172.25.8.14:8081/WasEngine/Audit/Alive" : "http://182.138.102.134:28080/WasEngine/Audit/Alive";
                        string rtnStr = HttpHelper.GetResponseWcf(wcfUri, "POST", shortJson, Encoding.UTF8);
                        LogHelper.WriteInfoLog(rtnStr);
                    });
                    //182.138.102.136 8081  telnet 182.138.102.134 28080
                }
                //survival
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("采集服务：方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
            }
        }
    }
}
